/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：CommandCopy.js<br>
 * 日期：2018年9月12日<br>
 * 作者: 孙燕宝<br>
 * 功能：添加图片命令<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.CommandHideAllImg<br>
 * 类型功能：添加图片命令<br>
 */
Rainier.UI.V2.CommandHideAllImg = draw2d.command.Command.extend({
	
	NAME : "Rainier.UI.V2.CommandHideAllImg",
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandHideAllImg.init<br>
	 * 函数功能：构造函数<br>
	 * 异常信息：无<br>
	 */
	init : function(context,editParts){
		this.context 		= context;
    	this.rootEditPart 	= this.context.getRootEditPart();
    	// 可隐藏的图片模型
    	this.imgArr = [];
    	// 已经隐藏的图片模型
    	this.deleteInfo = {};
    	this.deleteArr = [];
    	this.tabId = app.canvasId;
		this._super();
	},
	
    /**
	 * 函数名称：Rainier.UI.V2.CommandHideAllImg.execute<br>
	 * 函数功能：执行命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    execute:function(){
    	var _this =this;
    	//获取所有器件
		var children = 	this.rootEditPart.getModel().m_aChildren;
		$.each(children,function(index,child){
			var typeName = child.property.modelProperty.typeName;
			if(typeName=='CustomImg'){
				_this.imgArr.push(child);
			}
		});
		if(_this.imgArr.length>0){
			$.each(_this.imgArr,function(index,img){
				var parentModel = img.getParent();
				_this.deleteInfo = parentModel.removeChildModel(img);
				_this.deleteArr.push(_this.deleteInfo);
			});
		}
		if(typeof hideImgArr[this.tabId]!=='undefined'){
			hideImgArr[this.tabId] = hideImgArr[this.tabId].concat(this.deleteArr);
		}
		else{
			hideImgArr[this.tabId] = this.deleteArr;
		}
		//更新预览图
		// if(isSelectedPreview){
		// 	setTimeout(function(){
		// 		var img = $("#img-preview");
		// 		refreshPreview(img);
		// 	});
		// }
    },
    
    /**
	 * 函数名称：Rainier.UI.V2.CommandHideAllImg.undo<br>
	 * 函数功能：撤销命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    undo:function(){
    	if(this.deleteArr.length>0){
    		while(this.deleteArr.length>0){
    			this.deleteInfo = this.deleteArr[0];
    			this.showImg();
    		}
    	}
    },

     /**
	 * 函数名称：Rainier.UI.V2.CommandHideAllImg.showImg<br>
	 * 函数功能：重做命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    showImg:function(){
    	var _this = this;
    	if(this.deleteInfo){
			var currentModel 	 = this.deleteInfo.model;
			var connInfoStack	 = this.deleteInfo.deleteConnInfoStack.clone();
			if(currentModel instanceof Rainier.UI.V2.DeviceModel){
				var portList 	= this.deleteInfo.ports.clone();
				portList.each(function(index,port){
					currentModel.addPort(port);
				});

				var parentModel = currentModel.getParent();

				// 撤销时，修改自定义器件的图片
				currentModel.property.imageProperty.svg = currentModel.property.uiProperty.imageString;
				
				parentModel.addChildModel(currentModel);
				var uiProperty = currentModel.getProperty().uiProperty;
				currentModel.setPosition(uiProperty.x,uiProperty.y,false);
			}
			
			//翻转数组，逆序恢复数组中的内容
			connInfoStack.reverse();
			
			//恢复连线：
			connInfoStack.each(function(index,connModelInfo){
				_this.addConnectionModel(connModelInfo);
			})
			this.rootEditPart.refreshChildren();
		}
		var index = this.deleteArr.indexOf(this.deleteInfo);
		this.deleteArr.splice(index,1);
    },

    
    /**
	 * 函数名称：Rainier.UI.V2.CommandHideAllImg.redo<br>
	 * 函数功能：重做命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    redo:function(){
    	this.execute();
    },

     /**
	 * 函数名称：draw2d.command.CommandHideAllImg.addConnectionModel<br>
	 * 函数功能：恢复连线信息的添加<br>
	 * 参数输入：connModelInfo:封装好的connModelInfo信息<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	addConnectionModel : function(connModelInfo){
		var connType	= connModelInfo.connType;
		var connObj 	= connModelInfo.connModel;
		var sourceModel = connModelInfo.sourceModel;
		var targetModel = connModelInfo.targetModel;
		
		//第一步：把当前连线添加进来
		var conn 	   = connObj.connection;

		//第二步：恢复连线两端的jointModel
		if(sourceModel){
			var jointModel 		 = sourceModel.jointModel;
			var hybridPort		 = sourceModel.hybirdPort;
			
			var jointModelId	 = jointModel.getUuid();
			
			var jointModelParent  = jointModel.getParent();
			var connJointModelIds = [];
			var connJointModels   = jointModelParent.getChildren();
			connJointModels.forEach(function(connJointModel,index,connJointModels){
				connJointModelIds.push(connJointModel.getUuid());
			})
			//不存在当前model
			if(connJointModelIds.indexOf(jointModelId) === -1){
				jointModel.addPort(hybridPort);
				jointModelParent.addChildModel(jointModel);    					
			}
		}
		
		if(targetModel){
			var jointModel 		 = targetModel.jointModel;
			var hybridPort		 = targetModel.hybirdPort;
			
			var jointModelId	 = jointModel.getUuid();
			
			var jointModelParent  = jointModel.getParent();
			var connJointModelIds = [];
			var connJointModels   = jointModelParent.getChildren();
			connJointModels.forEach(function(connJointModel,index,connJointModels){
				connJointModelIds.push(connJointModel.getUuid());
			})
			//不存在当前model
			if(connJointModelIds.indexOf(jointModelId) === -1){
				jointModel.addPort(hybridPort);
				jointModelParent.addChildModel(jointModel);    					
			}
		}
		
		//第三步：恢复连线和sourcePort和targetPort的关联关系。
		var sourcePort = connObj.sourcePort;
		var targetPort = connObj.targetPort;
		conn.attachSourcePort(sourcePort);
		conn.attachTargetPort(targetPort);
		sourcePort.attachOutgoing(conn);
		targetPort.attachIncoming(conn);
			
		var connParent = conn.getParent();
		connParent.addConnection(conn);
	},
});